<?php
class Category extends AppModel {

	var $name = 'Category';
	
	var $displayField = 'name';
	
	var $actsAs = array(
		'MultipleValidatable',
		'Tree'
	);
	
	/**
	 * ============================================== VALIDATIONS ==============================================
	 */
	/**
	 * Default validation ruleset
	 */
    var $validate = array();
    
	/**
	 * Custom validation rulesets
	 */
	var $validationSets = array(
        'admin_add' => array(
			'name' => array(
				'NameRule1' => array(
	    			'rule' => array('minLength', '1'),
					'required' => true,
					'last' => true
				),
				'NameRule2' => array(
	    			'rule' => array('custom', '/\S+/'),
					'last' => true
				),
		    	'NameRule3' => array(
			    	'rule' => array('checkUniqueness'),
				    'last' => true
				)
			)
    	),
    	'admin_addall' => array(
			'name' => array(
				'NameRule1' => array(
	    			'rule' => array('minLength', '1'),
					'required' => true,
					'last' => true
				),
				'NameRule2' => array(
	    			'rule' => array('custom', '/\S+/'),
					'last' => true
				),
		    	'NameRule3' => array(
			    	'rule' => array('checkUniqueness'),
				    'last' => true
				)
			)
    	),
		'admin_edit' => array(
			'id' => array(
				'IdRule1' => array(
	    			'rule' => array('minLength', '1'),
					'required' => true,
					'last' => true
				),
				'IdRule2' => array(
	    			'rule' => 'numeric',
					'last' => true
				),
				'IdRule3' => array(
	    			'rule' => array('comparison', '>', 0),
					'last' => true
				),
				'IdRule4' => array(
	    			'rule' => array('checkIdFieldValue'),
					'last' => true
				)
			),
			/*'is_active' => array(
				'IsActiveRule1' => array(
					'rule' => array('minLength', '1'),
					'required' => true,
					'last' => true
				),
				'IsActiveRule2' => array(
					'rule' => 'numeric',
					'last' => true
				)
			),*/
			'name' => array(
				'NameRule1' => array(
	    			'rule' => array('minLength', '1'),
					'required' => true,
					'last' => true
				),
				'NameRule2' => array(
	    			'rule' => array('custom', '/\S+/'),
					'last' => true
				),
		    	'NameRule3' => array(
			    	'rule' => array('checkUniqueness'),
				    'last' => true
				)
			)
    	)
    );
    
	//--------------------------- check metodi za 'rule' => array('checkMethodName') opciqta
    var $idFieldValue = null;
    function checkIdFieldValue() {
    	return ( $this->idFieldValue == $this->data[$this->name]['id'] );
	}
	
	function checkUniqueness($check) {
		// $data array is passed using the form field name as the key
		// have to extract the value to make the function generic
		$value = array_shift($check);
		
		// is modified check
		$checkIsModified = $this->query("
			SELECT
				count(1) AS `result`
			FROM
				`categories`
			WHERE
				`id` = '".mysql_real_escape_string($this->id)."'
					AND
				`name` = '".mysql_real_escape_string($value)."'
		", false);
		if(!empty($checkIsModified[0][0]["result"])) {
			return true;
		}
		
		$parent_id = $this->data[$this->name]['parent_id'];
		if(empty($parent_id)) {
			$parent_id = 0;
		}
		$checkQuery = $this->query("
			SELECT
				count(1) AS `total`
			FROM
				`categories`
			WHERE
				`parent_id` = '".mysql_real_escape_string($parent_id)."'
					AND
				(
					`name` = '".mysql_real_escape_string($value)."'
						OR
					`url_title` = '".mysql_real_escape_string(Inflector::slug(mb_strtolower($value, "UTF-8"), "-"))."'
				)
		", false);
		
		return empty($checkQuery[0][0]["total"]);
	}
	//--------------------------/ check metodi za 'rule' => array('checkMethodName') opciqta
		
	/**
	 * =============================================/ VALIDATIONS ==============================================
	 */
	
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasMany = array(
		'Listing' => array(
			'className' => 'Listing',
			'foreignKey' => 'category_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
?>